package com.another.common.utils.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public class FunctionUtil {

    /**
     * 根据指定key去重
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object,Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    public static void main(String[] args){
        Map<Object,Boolean> seen = new ConcurrentHashMap<>();
        System.out.println(seen.putIfAbsent("a", true));
        System.out.println(seen.putIfAbsent("a", true));

        List<String> list = new ArrayList<>();
        list.add("a,b,c");
        list.add("b,c,d,e");
        boolean a = list.stream().flatMap(item -> Arrays.stream(item.split(","))).distinct().noneMatch(item -> item.equals(""));
        System.out.println(a);

    }
}
